package com.eden.door.parsers;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.eden.door.constant.Constant_Sys;
import com.eden.door.entity.Door;
import com.eden.door.entity.DoorAttr;
import com.eden.door.entity.Formula;
import com.eden.door.entity.Option;

public class DoorParser {

	public List<Door> parse(File[] doorFiles) {
		List<Door> doorList = null;

		if (ArrayUtils.isEmpty(doorFiles))
			return doorList;
		else {
			doorList = new ArrayList<Door>();

			for (File f : doorFiles) {
				doorList.add(parse(f));
			}

			return doorList;
		}
	}

	
	public Door parse(File doorFile) {
		Door door = new Door();
		
		//读取基本属性
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(doorFile) ;
			
			Element root = document.getRootElement();
			for(Iterator<?> i = root.elementIterator();i.hasNext();){
				Element e = (Element) i.next() ;
				if (StringUtils.equals("name", e.getName())) {
					door.setName(e.getData().toString()) ; 
				} else if( StringUtils.equals("type", e.getName()) ) {
					door.setType(e.getData().toString()) ;
				} else if( StringUtils.equals("code", e.getName()) ) {
					door.setCode(e.getData().toString()) ;
				} else if( StringUtils.equals("material", e.getName()) ) {
					door.setMaterial(e.getData().toString()) ;
				} else if( StringUtils.equals("unit", e.getName()) ) {
					door.setUnit(e.getData().toString()) ;
				} else if( StringUtils.equals("description", e.getName()) ) {
					door.setDescription(e.getData().toString()) ;
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace() ;
		}
		
		door.setDoorAttrs(getDoorAttrs(doorFile)) ;
		
		door.setFormulaList(new FormulaParser().parse(doorFile, door)  ) ;
		
		return door;
	}

	/**
	 * 从门的配置文件中读取各属性的运算方法
	 * @param doorFile 门的配置文件
	 * @return 
	 */
	public List<? extends Formula> getFormulas(File doorFile){
		List<? extends Formula> formulaList = new ArrayList<Formula>() ; 
		
		
		
		return formulaList ;
	}
	
	/**
	 * 从XML文件中解析门的动态参数，例如总宽，总高，扣板等
	 * @param doorFile XML文件
	 * @return 返回以属性的code为键，属性类为值的map
	 */
	public Map<String , DoorAttr> getDoorAttrs(File doorFile) {
		SAXReader reader = new SAXReader();

		Map<String , DoorAttr> doorAttrMap = new HashMap<String, DoorAttr>() ;
		DoorAttr doorAttr = null;
		try {
			Document document = reader.read(doorFile) ;
			
			Element root = document.getRootElement();
			for(Iterator<?> i = root.elementIterator();i.hasNext();){
				Element entry = (Element) i.next() ;
				if(entry.getName().equals("doorAttrs")){
					for(Iterator<?> doorAttrIter = entry.elementIterator() ; doorAttrIter.hasNext() ;) {
						doorAttr = new DoorAttr() ;
						
						Element doorAttrElement = (Element) doorAttrIter.next() ;
						
						doorAttr.setName(doorAttrElement.element("name").getData().toString()) ;
						doorAttr.setCode(doorAttrElement.element("code").getData().toString()) ;
						
						doorAttr.setType(doorAttrElement.element("type").getData().toString()) ;
						
						Object value = doorAttrElement.element("value").getData() ;
						doorAttr.setValue(value==null ? null : value.toString()) ;
						//根据类型，设置属性的值
						if(StringUtils.equals(doorAttr.getType(), Constant_Sys.DOOR_ATTR_TYPE_SELECT)){
							doorAttr.setOptList( readObject(doorAttrElement) ) ;
						}
						
						doorAttr.setFill(doorAttrElement.element("fill").getData().toString()) ;
						doorAttr.setCompuOrder(Integer.valueOf(doorAttrElement.element("compuOrder").getData().toString()) ) ;
						doorAttr.setPrintOrder(Integer.valueOf(doorAttrElement.element("printOrder").getData().toString()) ) ;
						doorAttr.setShowOrder(Integer.valueOf(doorAttrElement.element("showOrder").getData().toString()) ) ;
						
						
						doorAttrMap.put(doorAttr.getCode() , doorAttr) ;
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace() ;
		}
		
		return doorAttrMap ;
	}

	
	/**
	 * 从一个XML节点中读取并转换为类
	 * @param e 一个XML节点
	 * @return 返回arraylist
	 */
	public List<Option> readObject(Element e){
		List<Option> opts = new ArrayList<Option>() ;
		Element list = e.element("select") ;
		
		Option option = null ;
		for(Iterator<?> i = list.elementIterator() ; i.hasNext() ;) {
			Element entry = (Element) i.next() ;
			option = new Option() ;
			
			option.setValue(entry.attributeValue("value")) ;
			option.setText(entry.getData().toString()) ;
			
			opts.add(option) ;
 		}
		
		return opts ;
	}
	
	
	public Door parse2Door(File doorXml) {
		Door door = new Door();

		return door;
	}

	public static void main(String[] args) {
	}
}
